﻿//-----------------------------------------------------------------------
// <copyright file="Projects.cs" company="Microsoft Corporation">
//     Copyright Microsoft Corporation. All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) This is sample code only, do not use in production environments.
// </copyright>
//-----------------------------------------------------------------------
namespace Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.ViewModels
{
    using System;
    using System.Linq;
    using Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.MVVMSupport;
    using Windows.ApplicationModel.DataTransfer;

    public class Projects : IShare
    {
        public Projects(int category)
        {
            this.Category = category;
            DataTransferManager.GetForCurrentView().DataRequested += this.Project_DataRequested;
            var selectedCategory = (from c in App.Database.Categories
                                    where c.Id == category
                                    select c).Single();

            this.ProjectCollection = selectedCategory.Projects;

            this.GoBack = new Command(() =>
                {
                    Navigation.GoBack();
                });

            this.GoToHome = new Command(() =>
            {
                Navigation.Navigate(typeof(Views.TreasureMapPage));
            });
        }

        public Projects(Tuple<int, Models.Project> values)
            : this(values.Item1)
        {
            this.SelectedProject = values.Item2;
        }

        /// <summary>
        /// Gets the command for the home button
        /// </summary>
        public Command GoToHome { get; private set; }

        /// <summary>
        /// Gets or sets the Category of a project.
        /// </summary>
        public int Category { get; set; }

        /// <summary>
        /// Gets or sets the collection of projects associated with this project.
        /// </summary>
        public Models.Project[] ProjectCollection { get; set; }

        /// <summary>
        /// Gets or sets the currently selected project.
        /// </summary>
        public Models.Project SelectedProject { get; set; }

        /// <summary>
        /// Gets or sets the go back command.
        /// </summary>
        public Command GoBack { get; set; }

        /// <summary>
        /// Share request handler.
        /// </summary>
        /// <param name="sender">This parameter is not used.</param>
        /// <param name="args">This paramter is not used.</param>
        public void Project_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            args.Request.Data.Properties.Title = "ALM Readiness Treasure Map";
            args.Request.Data.Properties.Description = "Treasure from the " + this.SelectedProject.Name;
            var message = string.Format(
                          "{0}{1}{2}{1}{1}Guidance:",
                          this.SelectedProject.Name, 
                          Environment.NewLine, 
                          this.SelectedProject.Description);

            foreach (var item in this.SelectedProject.Guidance)
            {
                message += string.Format("{0}: {1}{2}", item.ProjectName, item.Url, Environment.NewLine);
            }

            args.Request.Data.SetText(message);
        }

        /// <summary>
        /// De-registers the share handler
        /// </summary>
        public void Deregister()
        {
            DataTransferManager.GetForCurrentView().DataRequested -= this.Project_DataRequested;
        }
    }
}
